package slisfx.user;

import java.util.ArrayList;
import java.util.ListIterator;
import slisfx.Globals;

public final class Userbase implements Globals{
	
	private ArrayList<Users> userKeeper = new ArrayList<Users>();
	private static final Userbase userBaseFinal = new Userbase();
	
	public static Userbase getUserbase(){
		return userBaseFinal;
	}

	public void add_user(Users newUser){
		userKeeper.add(newUser);
	}
	
	public void remove_user(String user){
            ListIterator<Users> it = Userbase.getUserbase().listIterator();
            while(it.hasNext()){
            //for (ListIterator<Users> it = Userbase.getUserbase().listIterator(); it.hasNext();) {
                Users u = it.next();
                if (u.getUserName().compareTo(user) == 0) {
                    it.remove();
                }
            }
            
//                for(Users u: userKeeper){
//                    if(u.getUserName().compareTo(user)== 0){
//                        userKeeper.remove(u);
//                    }
//                }
	}
	
	public int check_user(String user, String password) {
		for(Users u: userKeeper){
			if(u.getUserName().compareTo(user) == 0){
				if(u.getPassword().compareTo(password) == 0){
                                    return u.getUserPermissions();                                      
				}
			}
		}
		return INCORRECT_USERNAME_PASSWORD;
	}
        
        /* Returns true if the user exists*/
        public boolean admin_check_user(String user){
            for(Users u: userKeeper){
                if(u.getUserName().compareTo(user)== 0){
                    return true;
                }
            }
            return false;
        }
        
       public boolean admin_modify_permissions(String user, String perm){
            for(Users u: userKeeper){
                if(u.getUserName().compareTo(user)== 0){
                    u.setUserPermissions(new Integer(perm));
                    return true;
                }
            }
            return false;
        }
        
        	/**
	 * Creates XML
	 * @return xmlstring
	 */
	public String toXML(){	
		String xmlString = "<USERBASE>\n";
		for(Users u: userKeeper){
			xmlString += u.toXML();
		}
		return xmlString + "</USERBASE>"; 	
	}

    private ListIterator<Users> listIterator() {
        return userKeeper.listIterator();
    }
	

}
